//
//  WePersonalTableHeaderView.m
//  WeGalaxy
//
//  Created by Zheng on 2017/7/6.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "WePersonalTableHeaderView.h"

@implementation WePersonalTableHeaderView

#pragma mark - 点击事件
- (void)buttonClickAction:(UIButton *)sender {
    
    CGFloat x = sender == _evaluationBtn ? kSCREEN_WIDTH/2.f: 0;
    WePersonalHeaderType type = sender == _evaluationBtn? WePersonalHeaderTypeEvaluation:WePersonalHeaderTypePublish;
    
    [UIView animateWithDuration:kANIMATION_DURATION animations:^{
        _selectedView.frame = (CGRect){x,40,kSCREEN_WIDTH/2.f,10};
    }];
    
    _selectedBlock(type);

}

#pragma mark - 初始化
- (instancetype)init {
    self = [super init];
    if (self) {
        [self subviewsInView];
    }
    return self;
}

#pragma mark - 子视图
- (void)subviewsInView {
    [self addSubview:self.publishBtn];
    [self addSubview:self.evaluationBtn];
    [self addSubview:self.selectedView];
    [self addSubview:self.lineView];
    [self addSubview:self.bLineView];
    
    [_publishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self);
        make.width.equalTo(self).multipliedBy(0.5);
    }];
    
    [_evaluationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(self);
        make.left.equalTo(_publishBtn.mas_right);
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    
    [_bLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
}

- (UIView *)bLineView {
    if (!_bLineView) {
        _bLineView = [UIView new];
        _bLineView.backgroundColor = kRGB_COLOR(200, 199, 204, 1);
    }
    return _bLineView;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = kRGB_COLOR(200, 199, 204, 1);
    }
    return _lineView;
}

- (UIButton *)publishBtn {
    if (!_publishBtn) {
        _publishBtn = [self getHeaderButton:@"发布"];
    }
    return _publishBtn;
}

- (UIButton *)evaluationBtn {
    if (!_evaluationBtn) {
        _evaluationBtn = [self getHeaderButton:@"评价"];
    }
    return _evaluationBtn;
}

- (UIView *)selectedView {
    if (!_selectedView) {
        _selectedView = [[UIView alloc] initWithFrame:(CGRect){0,40,kSCREEN_WIDTH/2.f,10}];
        _selectedView.backgroundColor = kRGB_COLOR(245, 66, 59, 1);
    }
    return _selectedView;
}


- (UIButton *)getHeaderButton:(NSString *)title {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = kCX_FONT(15);
    return btn;
}



@end
